package com.sm.filemanagelibrary.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * created by zhud on 2019/9/27
 */
public class FileTypeUtil {
    private static Map<String, Type> typeMap = new HashMap<>();

    static {
        typeMap.put("image", Type.Image);
        typeMap.put("png", Type.Image);
        typeMap.put("jpg", Type.Image);
        typeMap.put("jpeg", Type.Image);
        typeMap.put("bmp", Type.Image);
        typeMap.put("gif", Type.Image);
        typeMap.put("tiff", Type.Image);
        typeMap.put("pdf", Type.Image);

        typeMap.put("avi", Type.Video);
        typeMap.put("mpg", Type.Video);
        typeMap.put("mpeg", Type.Video);
        typeMap.put("wmv", Type.Video);
        typeMap.put("rm", Type.Video);
        typeMap.put("rmvb", Type.Video);
        typeMap.put("mov", Type.Video);
        typeMap.put("mp4", Type.Video);
        typeMap.put("3gp", Type.Video);
        typeMap.put("swf", Type.Video);
        typeMap.put("flv", Type.Video);
        typeMap.put("f4v", Type.Video);
        typeMap.put("video", Type.Video);

        typeMap.put("wav", Type.Audio);
        typeMap.put("midi", Type.Audio);
        typeMap.put("wma", Type.Audio);
        typeMap.put("mp3", Type.Audio);
        typeMap.put("audio", Type.Audio);

        typeMap.put("txt", Type.Text);
        typeMap.put("html", Type.Html);

        typeMap.put("zip", Type.Package);
        typeMap.put("rar", Type.Package);
        typeMap.put("7z", Type.Package);

        typeMap.put("doc", Type.Document);
        typeMap.put("docx", Type.Document);
        typeMap.put("word", Type.Document);
        typeMap.put("dot", Type.Document);
        typeMap.put("dotx", Type.Document);
        typeMap.put("docm", Type.Document);
        typeMap.put("wps", Type.Document);

        typeMap.put("apk", Type.APP);


        typeMap.put("xls", Type.Form);
        typeMap.put("xlsx", Type.Form);
        typeMap.put("xlsm", Type.Form);
        typeMap.put("xps", Type.Form);
    }

    public enum Type {
        Image,
        Video,
        Audio,
        Text,
        Package,
        Document,
        Form,
        Html,
        APP,
        Unknown
    }

    public static Type toGetFileType(String typeStr) {
        Type type;
        if (TextUtils.isEmpty(typeStr)) {
            type = Type.Unknown;
        } else {
            type = typeMap.get(typeStr);
            if (type == null) {
                for (String mtype : typeMap.keySet()) {
                    if (typeStr.contains(mtype)) {
                        return typeMap.get(mtype);
                    }
                }
                return Type.Unknown;
            }
        }
        return type;
    }


    /**
     * 指定路径的apk是否安装
     *
     * @param context
     * @param packPath：应用安装包路径
     * @return
     */
    public static boolean isAvilible(Context context, String packPath) {
        final PackageManager packageManager = context.getPackageManager();
        PackageInfo info = packageManager.getPackageArchiveInfo(packPath, PackageManager.GET_ACTIVITIES);
        if (info == null) return false;
        ApplicationInfo appInfo = info.applicationInfo;
        String packageName = appInfo.packageName;  //得到安装包名称
        try {
            packageManager.getPackageInfo(appInfo.packageName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            //获取所有已安装程序的包信息
            List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
            //用于存储所有已安装程序的包名
            List<String> packageNames = new ArrayList<>();
            //从pinfo中将包名字逐一取出，压入pName list中
            if (packageInfos != null) {
                for (int i = 0; i < packageInfos.size(); i++) {
                    String packName = packageInfos.get(i).packageName;
                    packageNames.add(packName);
                }
            }
            //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
            return packageNames.contains(packageName);
        }
    }

    public static PackageInfo getPackageInfo(Context context, String packPath) {
        try {
            final PackageManager packageManager = context.getPackageManager();
            return packageManager.getPackageArchiveInfo(packPath, PackageManager.GET_ACTIVITIES);
        } catch (Exception e) {
            return null;
        }
    }

    //获取app信息
    public static String getPackageName(Context context, String packPath) {
        try {
            final PackageManager packageManager = context.getPackageManager();
            PackageInfo info = packageManager.getPackageArchiveInfo(packPath, PackageManager.GET_ACTIVITIES);
            if (info == null) {
                return Uri.parse(packPath).getLastPathSegment();
            }
            return info.packageName;
        } catch (Exception e) {
            return null;
        }
    }
}
